﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Data.Linq;


namespace ConsoleApplication1
{
    public class Program
    {

        public static void Main(string[] args)
        {
            //正确写法应该是正则表达式

            #region 写入省份数据到数据库
            ProvinceSchoolDBDataContext DB = new ProvinceSchoolDBDataContext();
            List<T_Province> listProvince = new List<T_Province>();
            using (StreamReader reader = new StreamReader("school_1.txt", Encoding.Default))
            {
                var allText = reader.ReadToEnd().TrimEnd('|');
                String[] onelines = allText.Split('|');
                for (int i = 0; i < onelines.Length; i++)
                {
                    String oneline = onelines[i];
                    String[] id_names = oneline.Split('@');
                    for (int j = 0; j < id_names.Length; j += 2)
                    {
                        var parm_1 = id_names[j].Split(':');
                        Console.Write("ID:  " + parm_1[1] + "             ");

                        var parm_2 = id_names[j + 1].Split(':');
                        Console.Write("Name:  " + parm_2[1]);
                        Console.WriteLine();

                        T_Province province = new T_Province();
                        province.provinceID = Int32.Parse(parm_1[1]);
                        province.provinceName = parm_2[1];
                        listProvince.Add(province);
                    }
                }
            }

           
            DB.T_Province.InsertAllOnSubmit(listProvince);
            DB.SubmitChanges();
            #endregion

            #region 写入学校数据到数据库

            //ProvinceSchoolDBDataContext DB = new ProvinceSchoolDBDataContext();
            //List<T_School> listSchool = new List<T_School>();
            //// ParentID:1@ID:1002#NAME:北京大学|
            //// ParentID:1 ID:1002#NAME:北京大学|
            //// ParentID:1 ID:1002 NAME:北京大学|
            //// ParentID 1 ID 1002 NAME 北京大学 
            //var countDemo = 0;
            //using (StreamReader reader = new StreamReader("school_2.txt", Encoding.Default))
            //{
            //    var allText = reader.ReadToEnd().TrimEnd('|');
            //    String[] onelines = allText.Split('|');
            //    for (int i = 0; i < onelines.Length; i++)
            //    {
            //        String oneline = onelines[i];// ParentID:1@ID:1002#NAME:北京大学 
            //        String[] parent_id_names = oneline.Split('@');// ParentID:1     ID:1002#NAME:北京大学
            //        for (int j = 0; j < parent_id_names.Length; j += 2)
            //        {
            //            string[] id_names = parent_id_names[j + 1].Split('#'); //ID:1002    NAME:北京大学
            //            for (int k = 0; k < id_names.Length; k += 2)
            //            {
            //                string parentID = parent_id_names[j].Split(':')[1]; //ParentID:1
            //                string school_id = id_names[k].Split(':')[1];//ID:1002   
            //                string school_name = id_names[k + 1].Split(':')[1];// NAME:北京大学

            //                T_School school = new T_School();
            //                school.provinceID = Int32.Parse(parentID);
            //                school.schoolID = Int32.Parse(school_id);
            //                school.schoolName = school_name;
            //                countDemo++;
            //                listSchool.Add(school);
            //                Console.WriteLine("ParentID:{0}     ID:{1}      Name:{2}", parentID, school_id, school_name);
            //            }
            //        }
            //    }
            //}


            //Console.WriteLine("总项数:" + countDemo.ToString());
            //DB.T_School.InsertAllOnSubmit(listSchool);

            ////DB.SubmitChanges(); 
            #endregion

            Console.ReadKey();

        }
    }
}

